package com.kuang.controller;

import com.kuang.pojo.SystemEmployee;
import com.kuang.service.EmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    EmployeeService employeeService;

    @RequestMapping("/user/login")
    //@ResponseBody    只能返回对象,不走解析器了就  要去掉
    public String login(@RequestParam("username") String username , @RequestParam("password") String password , Model model,
                        HttpSession session){
        //具体的业务
        SystemEmployee employee = employeeService.getEmployeeByCode(username);
        if (employee==null){
            //告诉用户 , 你登录失败了 , Model传值
            model.addAttribute("msg" , "账号不存在");
            return "index";
        }else {
            if(StringUtils.equals(employee.getPwd(),password)){
                //return "dashboard";
                session.setAttribute("loginUser",username);
                return "redirect:/main.html";
            }else{
                //告诉用户 , 你登录失败了 , Model传值
                model.addAttribute("msg" , "密码错误");
                return "index";
            }
        }

    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(@RequestBody String json){
        return json;
    }

}
